package com.alibaba.datax.core.transport.transformer;

import com.alibaba.datax.common.element.Column;
import com.alibaba.datax.common.element.Record;
import com.alibaba.datax.common.element.StringColumn;
import com.alibaba.datax.common.exception.DataXException;
import com.alibaba.datax.core.util.EncryptionSm4;
import com.alibaba.datax.transformer.Transformer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Sm4Transformer extends Transformer {

    private static final Logger LOG = LoggerFactory.getLogger(Sm4Transformer.class);

    public Sm4Transformer() {
        setTransformerName("dx_sm4");
    }

    @Override
    public Record evaluate(Record record, Object... paras) {
        int columnIndex = (Integer) paras[0];
        // 加密字段
        Column column = record.getColumn(columnIndex);
        try {
            String oriValue = column.asString();
            //如果字段为空，跳过，不进行解密操作
            if (StringUtils.isBlank(oriValue)) {
                return record;
            }
            String inStr = column.asString();
            String realStr = EncryptionSm4.decrypt(inStr);
            String outStr = EncryptionSm4.encryptionGbk(realStr);
            System.out.println("-----转译------>" + inStr + "::" + realStr + "::" + outStr);
            record.setColumn(columnIndex, new StringColumn(outStr));
        } catch (Exception e) {
            e.printStackTrace();
            throw DataXException.asDataXException("转译失败！");
        }
        return record;
    }
}
